package org.lanqiao.tjut.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.tjut.bean.StuBean;
import org.lanqiao.tjut.myUtils.MyUtils;
import org.lanqiao.tjut.service.ServiceFactory;

public class GradeModifyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 准备用户进行修改的数据
		String strOperationType = request.getParameter("op");
		int iOp = MyUtils.obj2Int(strOperationType);
		// 结果跳转页面
		String strJumpPage = "";

		if (iOp == 0) {
			// 修改
			// 修改跳转操作

			// 获取id主键值
			String strUid = request.getParameter("uid");
			StuBean stuB = ServiceFactory.getGradeManagerServiceInstance().getGradeById(strUid);
			// 将数据缓存到session对象中
			request.setAttribute("stuB", stuB);
			/*// 修改显示性别选项
			if(usersB.getUsersex().equals("男")){
				request.setAttribute("userSexM", "checked");
				//清空nv的标志变量
				request.setAttribute("userSexF", "");
			}else{
				request.setAttribute("userSexF", "checked");
				request.setAttribute("userSexM", "");
			}*/
			
			// 跳转修改结果页
			strJumpPage = "logined/UserInfoManager/userInfoGrade.jsp";
		} else if (iOp == 1) {
			// 删除
			int stuId = MyUtils.obj2Int(request.getParameter("uid"));
			//数据库删除
			int i = ServiceFactory.getGradeManagerServiceInstance().deleteStuById(stuId);
			//跳转页面
			strJumpPage = "GradeModifyServlet?op=2";
		} else if (iOp == 2) {
			// 判定当前的操作是查询还是修改跳转
			// 查询操作
			// 2、将保单数据填充到实体类
			StuBean stuB = (StuBean) MyUtils.getNewInstance(StuBean.class);
			
			stuB.setSTUNAME(request.getParameter("txtStuName"));
			stuB.setSTUCHI(MyUtils.obj2Integer(request.getParameter("txtStuChi")));
			stuB.setSTUMAT(MyUtils.obj2Integer(request.getParameter("txtStuMat")));
			stuB.setSTUENG(MyUtils.obj2Integer(request.getParameter("txtStuEng")));
			stuB.setSTUSCI(MyUtils.obj2Integer(request.getParameter("txtStuSci")));
			//条件查询用
			stuB.setSTUCHI2(MyUtils.obj2Integer(request.getParameter("txtStuChi2")));
			stuB.setSTUMAT2(MyUtils.obj2Integer(request.getParameter("txtStuMat2")));
			stuB.setSTUENG2(MyUtils.obj2Integer(request.getParameter("txtStuEng2")));
			stuB.setSTUSCI2(MyUtils.obj2Integer(request.getParameter("txtStuSci2")));

			// 查询操作
			List<StuBean> lstUsers = ServiceFactory.getGradeManagerServiceInstance().doQueryByParas(stuB);

			// 将需要显示的结果集放入session缓存中
			request.getSession().setAttribute("lstUsers", lstUsers);
			//查询结果页
			strJumpPage = "logined/UserInfoManager/userInfoGradeDetail.jsp";
		}
		// 跳转到修改页面
		request.getRequestDispatcher(strJumpPage).forward(request, response);
	}
}
